#import "FlagsGame.h"
#import "LabelGameComponent.h"
#import "ImageGameComponent.h"
#import "Game.h"
@interface FlagsGame ()

@property BOOL isGameOver;
@property NSArray* countries;
@property NSArray* colors;
@property NSMutableArray* flags;
@property NSString* currentCountry;
@property NSString* currentFlag;


@end

@implementation FlagsGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"FlagsGame"];
        [self setType:knowleadge];
        self.ruleOfTheGame=@"Is the flags correct?";
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        self.countries = [NSMutableArray arrayWithObjects:
                          @"Japan", @"United-States-of-America",@"Brazil", @"Germany", @"Russia",
                          @"France", @"Canada", @"China", @"Italy", @"Australia",
                          @"Uganda",@"Vatican-City",@"Costa-Rica",@"Fiji", @"Iceland",
                          @"India", @"Kenya", @"Lebanon", @"Bulgaria", @"Pakistan",
                          @"Poland", @"Saudi-Arabia", @"Samoa", @"Sweden",nil];
        self.flags = [NSMutableArray array];
        
        for (int i = 0; i < [self.countries count]; i++) {
            NSString *imageName = [NSString stringWithFormat:@"%@.png", [self.countries objectAtIndex:i]];
            [self.flags addObject:imageName];
        }
        
    }
    return self;
}

-(void)draw{
    [self removeAllComponents];
    
    
    int chanceToMatch = arc4random_uniform(kMaxNumberOfWrongAnswer);
    
    if (chanceToMatch == 0) {
        int index = arc4random_uniform(kFlagsGameNumberOfFlags);
        self.currentCountry = [self.countries objectAtIndex:index];
        self.currentFlag = [self.flags objectAtIndex:index];
    }
    
    else {
        
        int indexOfCountry = arc4random_uniform(kFlagsGameNumberOfFlags);
        int indexOfFlag = arc4random_uniform(kFlagsGameNumberOfFlags);
        
        self.currentCountry = [self.countries objectAtIndex:indexOfCountry];
        self.currentFlag = [self.flags objectAtIndex:indexOfFlag];
    }
    
    
    
    int color = arc4random_uniform(kColorNumber);
    
    NSString* labelText=[NSString stringWithFormat:@"%@",self.currentCountry];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:color]];
    
    
    NSString* labelOneText=[NSString stringWithFormat:@"%@",self.currentCountry];
    
    
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];
    
    [self addComponents:label];
    [self addComponents:labelOne];
    
    
    ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:self.currentFlag andFrame:CGRectMake(kScreenWidth/2.5, kScreenHeight/7.5, kScreenWidth/5, kScreenHeight/16)andTrans:M_PI];
    [self addComponents:image];
    ImageGameComponent *imageOne = [[ImageGameComponent alloc] initWithName:self.currentFlag andFrame:CGRectMake(kScreenWidth/2.5, kScreenHeight/2 - kButtonHeigh/2-10, kScreenWidth/5, kScreenHeight/16) andTrans:0];
    
    [self addComponents:image];
    [self addComponents:imageOne];
    
    
}

-(BOOL)isRight{
    if ([self.countries indexOfObject:self.currentCountry] == [self.flags indexOfObject:self.currentFlag]) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
     [NSTimer scheduledTimerWithTimeInterval: 2                                                 target: self selector:@selector(newDirection:)userInfo: nil repeats:YES];
    
}

-(void)newDirection:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}

@end
